//
//  TSLimitConvertHeaderView.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSLimitConvertItem: UIView {
    
    var priceLabel : UILabel!
    
    convenience init(_ title:String) {
        self.init()
        self.configViews(title: title)
    }
    
    func setupView(price:String) {
        
        priceLabel.attributedText = setupAttributedString(str: price)
    }
    
    func configViews(title:String) {
        
        priceLabel = UILabel.init()
        priceLabel.attributedText = setupAttributedString(str: title)
//        priceLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#454545")
//        priceLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        priceLabel.adjustsFontSizeToFitWidth = true
        priceLabel.minimumScaleFactor = 0.6
        priceLabel.textAlignment = .center
        self.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.top.equalTo(17)
            make.left.right.equalToSuperview().inset(5)
        }
    }
    
    func setupAttributedString(str:String) -> NSAttributedString {
        
        let tempStr = NSString.init(string: str)
        let tempArray = tempStr.components(separatedBy: "：")
        
        let attributeStr = NSMutableAttributedString.init(string: str)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:BASE_GRAY_COLOR, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13, weight: .medium)], range: NSMakeRange(0, str.count))
        
        attributeStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.hexadecimalColor(hexadecimal: "#3B3B3B"), range: tempStr.range(of: tempArray.last!))
        
        return attributeStr.copy() as! NSAttributedString
    }
}

class TSLimitConvertHeaderView: UIView {

    private var avatar : UIImageView!
    private var nameLabel : UILabel!
    private var identifyLabel : UILabel!
    private var storeItem : TSLimitConvertItem!
    private var coinItem : TSLimitConvertItem!
    private var profitItem : TSLimitConvertItem!
    private var gainItem : TSLimitConvertItem!

//    private var storeItem : TSWithDrawItem!
//    private var profitItem : TSWithDrawItem!
//    private var gainItem : TSWithDrawItem!
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F7F7F7")
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func refreshHeaaerView() {
        
        storeItem.setupView(price: "中心钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
        
        coinItem.setupView(price: "金币钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.jinbi ?? 0))
        
        profitItem.setupView(price: "盈利钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        
        gainItem.setupView(price: "收益钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
    }
    
    private func configViews() {
        
        let scale : CGFloat = 375 / 140.0
        let containerView = UIImageView.init(image: UIImage(named: "general_header_bg"))
        self.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(SCREEN_WIDTH / scale)
        }
        
        avatar = UIImageView.init()
        avatar.image = UIImage(named: "general_avatar_default")
        avatar.backgroundColor = WHITE_COLOR
        avatar.layer.cornerRadius = 30
        avatar.layer.masksToBounds = true
        containerView.addSubview(avatar)
        avatar.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.top.equalTo(26*T_ScreenScale())
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
        
        nameLabel = UILabel.init()
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂未设置"
        nameLabel.textColor = WHITE_COLOR
        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        containerView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar.snp.right).offset(13)
            make.top.equalTo(avatar.snp.top).offset(7*T_ScreenScale())
        }
        
        identifyLabel = UILabel.init()
        identifyLabel.text = "ID：\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        identifyLabel.textColor = WHITE_COLOR
        identifyLabel.font = UIFont.systemFont(ofSize: 12)
        containerView.addSubview(identifyLabel)
        identifyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(10*T_ScreenScale())
        }
        
        let itemContainer = UIView.init()
        itemContainer.backgroundColor = WHITE_COLOR
        itemContainer.layer.cornerRadius = 10
        itemContainer.layer.masksToBounds = true
        addSubview(itemContainer)
        itemContainer.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-15)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*15, height: 80))
        }
        
        storeItem = createItem(title: "中心钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
        itemContainer.addSubview(storeItem)
        
        coinItem = createItem(title: "金币钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.jinbi ?? 0))
        itemContainer.addSubview(coinItem)
        
        profitItem = createItem(title: "盈利钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        itemContainer.addSubview(profitItem)
        
        gainItem = createItem(title: "收益钱包："+String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
        itemContainer.addSubview(gainItem)
        
        let itemArray = [storeItem, coinItem, profitItem, gainItem]
        itemArray.snp.distributeSudokuViews(fixedLineSpacing: 0, fixedInteritemSpacing: 0, warpCount: 2)
        
        let line1 = UIView.init()
        line1.backgroundColor = BASE_LINE_COLOR
        itemContainer.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(5)
            make.centerY.equalToSuperview()
            make.height.equalTo(1)
        }

        let line2 = UIView.init()
        line2.backgroundColor = BASE_LINE_COLOR
        itemContainer.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview().inset(5)
            make.centerX.equalToSuperview()
            make.width.equalTo(1)
        }
        
    }
    
    private func createItem(title:String) -> TSLimitConvertItem {
        
        let item = TSLimitConvertItem.init(title)
        return item
    }
}
